home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wincap.zip / COPY.C next >
C/C++ Source or Header  |  1991-11-05  |  9KB  |  339 lines

  1. /*
  2.  *  copy.c
  3.  *
  4.  *  Source file for Device-Independent Bitmap (DIB) API.  Provides
  5.  *  the following functions:
  6.  *
  7.  *  CopyWindowToDIB()   - Copies a window to a DIB
  8.  *  CopyScreenToDIB()   - Copies entire screen to a DIB
  9.  *  CopyWindowToBitmap()- Copies a window to a standard Bitmap
  10.  *  CopyScreenToBitmap()- Copies entire screen to a standard Bitmap
  11.  *
  12.  *  The following functions are called from DIBUTIL.C:
  13.  *
  14.  *  DIBToBitmap()       - Creates a bitmap from a DIB
  15.  *  BitmapToDIB()       - Creates a DIB from a bitmap
  16.  *  DIBWidth()          - Gets the width of the DIB
  17.  *  DIBHeight()         - Gets the height of the DIB
  18.  *  CreateDIBPalette()  - Gets the DIB's palette
  19.  *  GetSystemPalette()  - Gets the current palette
  20.  *
  21.  * Development Team: Mark Bader
  22.  *                   Patrick Schreiber
  23.  *                   Garrett McAuliffe
  24.  *                   Eric Flo
  25.  *                   Tony Claflin
  26.  *
  27.  * Written by Microsoft Product Support Services, Developer Support.
  28.  * Copyright (c) 1991 Microsoft Corporation. All rights reserved.
  29.  */
  30.  
  31. /* header files */
  32. #include <WINDOWS.H>
  33. #include "ERRORS.H"
  34. #include "DIBUTIL.H"
  35. #include "DIBAPI.H"
  36.  
  37. /*************************************************************************
  38.  *
  39.  * CopyWindowToDIB()
  40.  *
  41.  * Parameters:
  42.  *
  43.  * HWND hWnd        - specifies the window
  44.  *
  45.  * WORD fPrintArea  - specifies the window area to copy into the device-
  46.  *                    independent bitmap
  47.  *
  48.  * Return Value:
  49.  *
  50.  * HDIB             - identifies the device-independent bitmap
  51.  *
  52.  * Description:
  53.  *
  54.  * This function copies the specified part(s) of the window to a device-
  55.  * independent bitmap.
  56.  *
  57.  ************************************************************************/
  58.  
  59.  
  60. HDIB CopyWindowToDIB(HWND hWnd, WORD fPrintArea)
  61. {
  62.    HDIB hDIB = NULL;  // handle to DIB
  63.  
  64.    /* check for a valid window handle */
  65.  
  66.    if (!hWnd)
  67.       return NULL;
  68.    switch (fPrintArea)
  69.       {
  70.    case PW_WINDOW: // copy entire window
  71.    {
  72.       RECT rectWnd;
  73.  
  74.       /* get the window rectangle */
  75.  
  76.       GetWindowRect(hWnd, &rectWnd);
  77.  
  78.       /*  get the DIB of the window by calling
  79.        *  CopyScreenToDIB and passing it the window rect
  80.        */
  81.       hDIB = CopyScreenToDIB(&rectWnd);
  82.    }
  83.       break;
  84.  
  85.    case PW_CLIENT: // copy client area
  86.    {
  87.       RECT rectClient;
  88.       POINT pt1, pt2;
  89.  
  90.       /* get the client area dimensions */
  91.  
  92.       GetClientRect(hWnd, &rectClient);
  93.  
  94.       /* convert client coords to screen coords */
  95.       pt1.x = rectClient.left;
  96.       pt1.y = rectClient.top;
  97.       pt2.x = rectClient.right;
  98.       pt2.y = rectClient.bottom;
  99.       ClientToScreen(hWnd, &pt1);
  100.       ClientToScreen(hWnd, &pt2);
  101.       rectClient.left = pt1.x;
  102.       rectClient.top = pt1.y;
  103.       rectClient.right = pt2.x;
  104.       rectClient.bottom = pt2.y;
  105.  
  106.       /*  get the DIB of the client area by calling
  107.        *  CopyScreenToDIB and passing it the client rect
  108.        */
  109.       hDIB = CopyScreenToDIB(&rectClient);
  110.    }
  111.       break;
  112.  
  113.    default:    // invalid print area
  114.       return NULL;
  115.       }
  116.  
  117.    /* return the handle to the DIB */
  118.    return hDIB;
  119. }
  120.  
  121.  
  122. /*************************************************************************
  123.  *
  124.  * CopyScreenToDIB()
  125.  *
  126.  * Parameter:
  127.  *
  128.  * LPRECT lpRect    - specifies the window
  129.  *
  130.  * Return Value:
  131.  *
  132.  * HDIB             - identifies the device-independent bitmap
  133.  *
  134.  * Description:
  135.  *
  136.  * This function copies the specified part of the screen to a device-
  137.  * independent bitmap.
  138.  *
  139.  ************************************************************************/
  140.  
  141.  
  142. HDIB CopyScreenToDIB(LPRECT lpRect)
  143. {
  144.    HBITMAP hBitmap;    // handle to device-dependent bitmap
  145.    HPALETTE hPalette;  // handle to palette
  146.    HDIB hDIB = NULL;   // handle to DIB
  147.  
  148.    /*  get the device-dependent bitmap in lpRect by calling
  149.     *  CopyScreenToBitmap and passing it the rectangle to grab
  150.     */
  151.  
  152.    hBitmap = CopyScreenToBitmap(lpRect);
  153.  
  154.    /* check for a valid bitmap handle */
  155.    if (!hBitmap)
  156.       return NULL;
  157.  
  158.    /* get the current palette */
  159.    hPalette = GetSystemPalette();
  160.  
  161.    /* convert the bitmap to a DIB */
  162.    hDIB = BitmapToDIB(hBitmap, hPalette);
  163.  
  164.    /* clean up */
  165.    DeleteObject(hBitmap);
  166.  
  167.    /* return handle to the packed-DIB */
  168.    return hDIB;
  169. }
  170.  
  171.  
  172. /*************************************************************************
  173.  *
  174.  * CopyWindowToBitmap()
  175.  *
  176.  * Parameters:
  177.  *
  178.  * HWND hWnd        - specifies the window
  179.  *
  180.  * WORD fPrintArea  - specifies the window area to copy into the device-
  181.  *                    dependent bitmap
  182.  *
  183.  * Return Value:
  184.  *
  185.  * HDIB         - identifies the device-dependent bitmap
  186.  *
  187.  * Description:
  188.  *
  189.  * This function copies the specified part(s) of the window to a device-
  190.  * dependent bitmap.
  191.  *
  192.  ************************************************************************/
  193.  
  194.  
  195. HBITMAP CopyWindowToBitmap(HWND hWnd, WORD fPrintArea)
  196. {
  197.    HBITMAP hBitmap = NULL;  // handle to device-dependent bitmap
  198.  
  199.    /* check for a valid window handle */
  200.  
  201.    if (!hWnd)
  202.       return NULL;
  203.    switch (fPrintArea)
  204.       {
  205.    case PW_WINDOW: // copy entire window
  206.    {
  207.       RECT rectWnd;
  208.  
  209.       /* get the window rectangle */
  210.  
  211.       GetWindowRect(hWnd, &rectWnd);
  212.  
  213.       /*  get the bitmap of that window by calling
  214.        *  CopyScreenToBitmap and passing it the window rect
  215.        */
  216.       hBitmap = CopyScreenToBitmap(&rectWnd);
  217.    }
  218.       break;
  219.  
  220.    case PW_CLIENT: // copy client area
  221.    {
  222.       RECT rectClient;
  223.       POINT pt1, pt2;
  224.  
  225.       /* get client dimensions */
  226.  
  227.       GetClientRect(hWnd, &rectClient);
  228.  
  229.       /* convert client coords to screen coords */
  230.       pt1.x = rectClient.left;
  231.       pt1.y = rectClient.top;
  232.       pt2.x = rectClient.right;
  233.       pt2.y = rectClient.bottom;
  234.       ClientToScreen(hWnd, &pt1);
  235.       ClientToScreen(hWnd, &pt2);
  236.       rectClient.left = pt1.x;
  237.       rectClient.top = pt1.y;
  238.       rectClient.right = pt2.x;
  239.       rectClient.bottom = pt2.y;
  240.  
  241.       /*  get the bitmap of the client area by calling
  242.        *  CopyScreenToBitmap and passing it the client rect
  243.        */
  244.       hBitmap = CopyScreenToBitmap(&rectClient);
  245.    }
  246.       break;
  247.  
  248.    default:    // invalid print area
  249.       return NULL;
  250.       }
  251.  
  252.    /* return handle to the bitmap */
  253.    return hBitmap;
  254. }
  255.  
  256.  
  257. /*************************************************************************
  258.  *
  259.  * CopyScreenToBitmap()
  260.  *
  261.  * Parameter:
  262.  *
  263.  * LPRECT lpRect    - specifies the window
  264.  *
  265.  * Return Value:
  266.  *
  267.  * HDIB             - identifies the device-dependent bitmap
  268.  *
  269.  * Description:
  270.  *
  271.  * This function copies the specified part of the screen to a device-
  272.  * dependent bitmap.
  273.  *
  274.  ************************************************************************/
  275.  
  276.  
  277. HBITMAP CopyScreenToBitmap(LPRECT lpRect)
  278. {
  279.    HDC hScrDC, hMemDC;           // screen DC and memory DC
  280.    HBITMAP hBitmap, hOldBitmap;  // handles to deice-dependent bitmaps
  281.    int nX, nY, nX2, nY2;         // coordinates of rectangle to grab
  282.    int nWidth, nHeight;          // DIB width and height
  283.    int xScrn, yScrn;             // screen resolution
  284.  
  285.    /* check for an empty rectangle */
  286.  
  287.    if (IsRectEmpty(lpRect))
  288.       return NULL;
  289.  
  290.    /*  create a DC for the screen and create
  291.     *  a memory DC compatible to screen DC
  292.     */
  293.    hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  294.    hMemDC = CreateCompatibleDC(hScrDC);
  295.  
  296.    /* get points of rectangle to grab */
  297.    nX = lpRect->left;
  298.    nY = lpRect->top;
  299.    nX2 = lpRect->right;
  300.    nY2 = lpRect->bottom;
  301.  
  302.    /* get screen resolution */
  303.    xScrn = GetDeviceCaps(hScrDC, HORZRES);
  304.    yScrn = GetDeviceCaps(hScrDC, VERTRES);
  305.  
  306.    /* make sure bitmap rectangle is visible */
  307.    if (nX < 0)
  308.       nX = 0;
  309.    if (nY < 0)
  310.       nY = 0;
  311.    if (nX2 > xScrn)
  312.       nX2 = xScrn;
  313.    if (nY2 > yScrn)
  314.       nY2 = yScrn;
  315.    nWidth = nX2 - nX;
  316.    nHeight = nY2 - nY;
  317.  
  318.    /* create a bitmap compatible with the screen DC */
  319.    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
  320.  
  321.    /* select new bitmap into memory DC */
  322.    hOldBitmap = SelectObject(hMemDC, hBitmap);
  323.  
  324.    /* bitblt screen DC to memory DC */
  325.    BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
  326.  
  327.    /*  select old bitmap back into memory DC and get handle to
  328.     *  bitmap of the screen
  329.     */
  330.    hBitmap = SelectObject(hMemDC, hOldBitmap);
  331.  
  332.    /* clean up */
  333.    DeleteDC(hScrDC);
  334.    DeleteDC(hMemDC);
  335.  
  336.    /* return handle to the bitmap */
  337.    return hBitmap;
  338. }
  339.